use axum::{
    body::Bytes,
    extract::{FromRequest, Request},
};
use serde::de::DeserializeOwned;

use crate::error::AppErr;

use super::Cbor;

impl<T: DeserializeOwned, S: Sync + Send> FromRequest<S> for Cbor<T> {

    type Rejection = AppErr;

    async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
        let body = Bytes::from_request(req, state).await?;
        let val: T = serde_cbor::from_slice(&body)?;
        Ok(Cbor(val))
    }
}
